/*
 * @Author: duanguang
 * @Date: 2020-05-26 14:00:08
 * @Last Modified by: duanguang
 * @Last Modified time: 2020-11-02 15:51:13
 */
import StoreBase, { IStoreBaseMeta } from '../../common/stores/StoreBase';
import { observable, autorun, action, computed } from 'mobx';
import { StoreModules } from 'legions/store';
import {
  observablePromise,
  ObservableTempState,
  observableViewModel,
} from 'legions/store-utils';
import { ProjectFormFields } from '../models/form/projectFormFields';
import { InstanceProForm } from 'legions-pro-design/es/LegionsProForm/interface';
import { InstanceProModal } from 'legions-pro-design/es/LegionsProModal/interface';
import { InstanceProTable } from 'legions-pro-design/es/LegionsProTable/interface';
import ProjectListPagePropertyStore from './projectListPageProperty';
import {
  OperationEnum,
  createProjectEnum,
  templatePageEnum,
} from '../constants/enum-data';
import ProjectFormPropertyStore from './projectFormProperty';
import ProjectCommonStore from './projectCommonStore';
interface IContext {
  formPropertyApp: ProjectFormPropertyStore;
  /**
   *
   * 列表页数据属性
   * @type {ProjectListPagePropertyStore}
   * @memberof IContext
   */
  listPagePropertyApp: ProjectListPagePropertyStore;
  projectCommonApp: ProjectCommonStore;
}
type ILoading = 'none' | 'pending' | 'complete' | 'error';
export type TStepCurr = 0 | 2 | 1;
class ViewModel {
  @computed get computedLoading(): boolean {
    if (this.loading === 'pending') {
      return true;
    }
    return false;
  }
  @computed get computedLoad(): ILoading {
    return this.loading;
  }

  /** * 项目当前编辑步骤 (包含项目信息填写->页面制作->生成页面) */
  @observable stepCurr: TStepCurr = 0;
  /** * 选中的仓库信息KEY*/
  @observable selectedWarehouseKey = '';

  /** ** 页面操作类型 预览和保存 */
  @observable operation: typeof OperationEnum[keyof typeof OperationEnum] = 1;

  @observable token = '';

  @observable colSpan = { left: 4, right: 20 };

  @observable private loading: ILoading = 'none';

  /** 创建项目操作类型 */
  @observable createProjectType: keyof typeof createProjectEnum = null;

  /** 当前模块类型，例如列表页或者表单页 */
  @observable currentTemplatePage: keyof typeof templatePageEnum = 'listPage';

  /** 创建的组件文件信息，主要用于重复生成同样代码文件 */
  @observable historyComponentsFileList: string[] = [];

  /** 画板模式，主要分为绘制及预览两种模式 */
  @observable sketchpadMode: 'draw' | 'preview' = 'draw';
  @action updateLoadingState(state: ILoading) {
    this.loading = state;
  }
  /** 设置项目数据类型 */
  @action setCreateProjectType(type: keyof typeof createProjectEnum) {
    this.createProjectType = type;
  }
  /** 设置当前需要生成的模块类型  */
  @action setCurrentTemplatePage(type: keyof typeof templatePageEnum) {
    this.currentTemplatePage = type;
  }
  /** 设置画板为预览状态 */
  @action setSketchpadPreviewMode() {
    this.sketchpadMode = 'preview';
    this.colSpan.left = 0;
    this.colSpan.right = 24;
  }
  /** 设置画板为绘制状态 */
  @action setSketchpadDrawMode() {
    this.sketchpadMode = 'draw';
    this.colSpan.left = 4;
    this.colSpan.right = 20;
  }
}
class BasicModalViewModel {
  modalRef: InstanceProModal = null;
}


/**
 *
 * 可视化数据 包含初始化项目数据
 * @export
 * @class ProjectStore
 * @extends {StoreBase<IContext>}
 */
@StoreModules
export default class ProjectStore extends StoreBase<IContext> {
  static meta: IStoreBaseMeta = {
    ...StoreBase.meta,
    eventScopes: [],
    contextTypes: {
      listPagePropertyApp: ProjectListPagePropertyStore,
      formPropertyApp: ProjectFormPropertyStore,
      projectCommonApp:ProjectCommonStore,
    },
  };
  /**
   * 表格列配置属性添加表单实例
   *
   * @type {InstanceForm}
   * @memberof ProjectStore
   */
  formProperyAddTableColumnsRef: InstanceProForm = null;

  /**
   * 表格数据源配置属性表单实例
   *
   * @type {InstanceForm}
   * @memberof ProjectStore
   */
  formProperyDataSourceRef: InstanceProForm = null;

  /**
   *
   *  表格基础配置属性表单实例
   * @type {InstanceForm}
   * @memberof ProjectStore
   */
  formProperyBaseConfigRef: InstanceProForm = null;

  /** 操作按钮配置属性表单实例 */
  formProperyOperattionRef: InstanceProForm = null;
  formProperKeyValueRef: InstanceProForm = null;

  /**
   * 项目编辑快速生成组件内部状态
   *
   * @memberof ProjectStore
   */
  projectEditViewModel = observableViewModel<ViewModel>(new ViewModel());

  /** 创建项目信息表单实例对象 */
  createProjectFormRef: InstanceProForm = null;

  /** 模态框数据模型 */
  basicModalViewModel = observableViewModel<BasicModalViewModel>(
    new BasicModalViewModel()
  );

  
}
